package concurrency.basicthreading.demo;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class Demo14NaiveExceptionHanding {

	/*
	 * 运行结果显示异常仍然没有被捕获。为了解决这个问题我们需要修改Executor产生线程的方式。
	 * Thread.UncaughtExceptionHandler是Java SE5中的新接口。该接口允许你在每个Thread对象上附着一个
	 * 异常处理器--即Thread.UncaughtExceptionHandler.uncaughtException()方法。该方法在线程发生异常而临近死亡时
	 * 被调用。为此我们修改ExceptionThread类。并创建一个新的类型ThreadFactory, 它将在每个新创建的Thread对象上附
	 * 着一个Thread.UncaughtExceptionHandler。然后将给类型传递给新Executors创建ExecutorService
	 */

	public static void main(String[] args) {
		try {
			ExecutorService exec = Executors.newCachedThreadPool();
			exec.execute(new Demo14ExceptionThread());
			exec.shutdown();
		} catch(RuntimeException e) {
			System.out.println("Exeception has been handled!");
		}
	}
}
